 package org.squareland;

import org.squareland.entities.BulletEntity;
import org.squareland.entities.DwarfEntity;
import org.squareland.entities.Entity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class SquarelandActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        wrapper = new RelativeLayout(this);
        leftController = new ControllerView(this);
		rightController = new ControllerView(this);
		scene = new SceneView(this);

		leftControllerLayout = new LayoutParams(150, 150);
		leftControllerLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		leftControllerLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		

		rightControllerLayout = new LayoutParams(150, 150);
		rightControllerLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		rightControllerLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

		sceneLayout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		
		wrapper.addView(scene, 0, sceneLayout);
		wrapper.addView(leftController, 1, leftControllerLayout);
		wrapper.addView(rightController, 2, rightControllerLayout);
		wrapper.setFocusable(true);
		wrapper.setClickable(true);
		wrapper.setFocusableInTouchMode(true);

		setContentView(wrapper);
	}
    
    @Override
    //TODO currentTimeMills depends on current time so
    // everything can fall on time change.
    protected void onStart() {
    	super.onStart();
    	run = true;
    	
    	// TODO make more safe thread
    	new Thread(new Runnable() {
			@Override
			public void run() {
	    		long loopStartTime = System.currentTimeMillis();
	    		long loopEndTime;
	    		long loopTotalTime = 0;
	    		Pointf point;
	    		DwarfEntity character = (DwarfEntity) scene.gameWorld.character;
	    		
	    		Entity enemy = new DwarfEntity();
				enemy.x = 15;
				enemy.y = 10;
				scene.gameWorld.world.add(enemy);
	    		
				while(run) {
		    		// TODO add synchronization
					try {
		    			// Make first controller control the direction
		    			point = new Pointf();
			    		leftController.getCoords(point);
			    		scene.gameWorld.character.speedX = point.x * 50.0f;
			    		scene.gameWorld.character.speedY = point.y * 50.0f;
			    		
			    		// Make second controller control bullets
			    		if (loopStartTime - character.lastFireTime > character.fireDelay) {
				    		point = new Pointf();
				    		
				    		rightController.getCoords(point);
				    		if (point.x != 0.0f && point.y != 0.0f) {
				    			character.lastFireTime = loopStartTime;
				    			BulletEntity bullet = new BulletEntity();
				    			bullet.x = scene.gameWorld.character.x;
				    			bullet.y = scene.gameWorld.character.y;
				    			bullet.speedX = point.x * 50;
				    			bullet.speedY = point.y * 50;
				    			bullet.generatedBy = character;
				    			scene.gameWorld.world.add(bullet);
				    		}
			    		}
			    		
			    		// Updating the world
			    		scene.gameWorld.updateWorld(loopTotalTime);
			    		scene.postInvalidate();
			    		synchronized(this) {
			    			wait(10);
			    		}
			    		
			    		loopEndTime = System.currentTimeMillis();
			    		loopTotalTime = (loopEndTime - loopStartTime);
			    		loopStartTime = loopEndTime;
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
    	}).start();
    }
	
    @Override
    protected void onPause() {
    	super.onPause();
    	run = false;
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	run = false;
    }
    
    private boolean run;
    private RelativeLayout wrapper;
	
	private ControllerView leftController;
	private LayoutParams leftControllerLayout;
	
	private ControllerView rightController;
	private LayoutParams rightControllerLayout;
	
	private SceneView scene;
	private LayoutParams sceneLayout;
}